package com.jf.cloud.multishop.controller.admin;

import cn.hutool.core.util.BooleanUtil;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.dto.ShopTemplateDTO;
import com.jf.cloud.multishop.model.ShopTemplate;
import com.jf.cloud.multishop.service.ShopTemplateService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 菠萝凤梨
 */
@RestController("adminShopTemplateUpDelController")
@RequestMapping("/mp/shop_template")
//@ConditionalOnProperty(prefix = "jfcloud_", name = "expose.operation.auth", havingValue = "true", matchIfMissing = true)
@Tag(name = "店铺装修模板-修改、删除、设为主页")
public class ShopTemplateUpDelController {

    @Autowired
    private ShopTemplateService shopTemplateService;

    @Autowired
    private MapperFacade mapperFacade;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @PutMapping("/update_pc")
    @Operation(summary = "更新商品PC装修模板表" , description = "更新商品PC装修模板表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("没有权限进行操作");
        }
        updateTemplate(shopTemplateDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_h5")
    @Operation(summary = "更新商品H5装修模板表" , description = "更新商品H5装修模板表")
    public ServerResponseEntity<Void> updateH5(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("没有权限进行操作");
        }
        updateTemplate(shopTemplateDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("delete_pc")
    @Operation(summary = "删除商品PC装修模板表" , description = "根据商品装修模板表id删除商品PC装修模板表")
    public ServerResponseEntity<Void> delete(@RequestParam Long templateId) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("没有权限进行操作");
        }
        deleteTemplate(templateId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("delete_h5")
    @Operation(summary = "删除商品H5装修模板表" , description = "根据商品装修模板表id删除商品H5装修模板表")
    public ServerResponseEntity<Void> deleteH5(@RequestParam Long templateId) {
        if (BooleanUtil.isFalse(permission)){
            throw new LuckException("没有权限进行操作");
        }
        deleteTemplate(templateId);
        return ServerResponseEntity.success();
    }

    private void deleteTemplate(Long templateId) {
        ShopTemplate shopTemplateDb = shopTemplateService.getByTemplateId(templateId);
        if (Objects.isNull(shopTemplateDb)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplateDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplateService.deleteById(templateId);
        shopTemplateService.removeCache(templateId);
    }

    private void updateTemplate(ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = mapperFacade.map(shopTemplateDTO, ShopTemplate.class);
        ShopTemplate shopTemplateDb = shopTemplateService.getByTemplateId(shopTemplate.getTemplateId());
        if (Objects.isNull(shopTemplateDb)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplateDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplateService.update(shopTemplate);
        shopTemplateService.removeCache(shopTemplate.getTemplateId());
    }
}
